<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
    /**
     * 手动增送优惠券
     */
	<shiro:hasPermission name="back_gotoDiscountCouponPage_sendCouponsManually">
    function sendCouponsManually() {
        /**获取选中行的id**/
        var selectedRow = $("#tt").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            if (selectedRow[0].isPass==2){
				if (selectedRow[0].useStatus==1){
					$("#btn_sendCouponsManually").attr("data-target", "#sendCouponsManuallyModal");/**选中了记录条件符合后才可打开对应的模态框**/
                    /** 重置当前未选中的列表中的查询条件**/
                    $("#qform_sub")[0].reset();
                    /** 刷新当前未选中的列表**/
                    $("#tt_customerList").bootstrapTable('refresh');
                    $("#s_discountCouponId").val(selectedRow[0].discountCouponId);
                    sendCouponsManuallyList();
				}else if (selectedRow[0].useStatus==0){
                    $("#btn_sendCouponsManually").attr("data-target", "");/**条件不符不可打开模态框**/
                    showMsg("优惠券处于未启用状态！");
            	}else if (selectedRow[0].useStatus==2){
                    $("#btn_sendCouponsManually").attr("data-target", "");/**条件不符不可打开模态框**/
                    showMsg("优惠券处于已过期状态！");
                }
			}else if (selectedRow[0].isPass==1){
                $("#btn_sendCouponsManually").attr("data-target", "");/**条件不符不可打开模态框**/
                showMsg("优惠券处于待审核状态！");
			}else if (selectedRow[0].isPass==3){
                $("#btn_sendCouponsManually").attr("data-target", "");/**条件不符不可打开模态框**/
                showMsg("优惠券处于审核未通过状态！");
            }
        }else {
            $("#btn_sendCouponsManually").attr("data-target", "");/**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
    </shiro:hasPermission>

    /**
	 * 手动赠送优惠券的会员列表
     */
    function sendCouponsManuallyList() {
        var $tt_customerList = $("#tt_customerList").bootstrapTable({
            url: '${basePath}/back/customer/listCustomer.sjson',       	/**请求后台的URL（*）**/
            method: 'post',                       	/**请求方式（*）**/
            contentType: "application/x-www-form-urlencoded",
            dataType: 'json',
            striped: true,                      	/**是否显示行间隔色**/
            cache: false,                       	/**是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）**/
            pagination: true,                   	/**是否显示分页（*）**/
            sortable: false,                    	/**是否启用排序**/
            sortOrder: 'asc',                   	/**排序方式**/
            queryParamsType: '',
            sidePagination: 'server',           	/**分页方式：client客户端分页，server服务端分页（*）**/
            pageNumber: 1,
            pageSize:10,							/**初始化加载第一页，默认第一页**/
            pageList: [5,10, 25, 50],        	    /**可供选择的每页的行数（*）**/
            search: false,                       	/**是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大**/
            strictSearch: true,
            showColumns: false,                  	/**是否显示所有的列**/
            showRefresh: false,                  	/**是否显示刷新按钮**/
            minimumCountColumns: 2,             	/**最少允许的列数**/
            clickToSelect: true,                	/**是否启用点击选中行**/
            uniqueId: 'customerId',           		/**每一行的唯一标识，一般为主键列**/
            singleSelect  : true,                   /** 单选checkbox**/
            showToggle:false,                    	/**是否显示详细视图和列表视图的切换按钮**/
            cardView: false,                    	/**是否显示详细视图**/
            detailView: false,                  	/**是否显示父子表**/
            columns: [
                {checkbox: true},
                {field: 'Number', title: '编号', align: 'center', valign: 'middle', width: '50px',
                    formatter: function (value, row, index) {
                        var page = $tt_customerList.bootstrapTable("getPage");
                        return page.pageSize * (page.pageNumber - 1) + index + 1;
                    }
                },
                {field: 'loginName', title: '会员名称', align: 'center', valign: 'middle'},
                {field: "sex", title: "性别", align: 'center', valign: 'middle',
                    formatter:function(value,row,index){
                        var sexStr = "-";
                        <c:forEach items="${redis:getBasicKeyBook('sex')}" var="kb">
                        if(value=="${kb.value}"){
                            sexStr="${kb.name}";
                        }
                        </c:forEach>
                        return sexStr;
                    }
				},
                {field: "email", title: "电子邮箱", align: 'center', valign: 'middle'},
                {field: "phone", title: "手机号", align: 'center', valign: 'middle'},
                {field: "lockedState", title: "状态", align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if(value=="0"){ return "<font class='color_001'>未冻结</font>";}
                        if(value=="1"){ return "<font class='color_002'>已冻结</font>";}
                    }
                },
                {field: "wxUserOpenId", title: "关联微信", align: 'center', valign: 'middle',
                    formatter:function(value,row,index){
                        if(value!=undefined&&value!=''){
                            return "<font class='color_001'>已关联</font>";
                        }
                        else{
                            return "<font class='color_002'>未关联</font>";
                        }
                    }
                }
            ],
            queryParams: function (params) {
                /**特别说明，返回的参数的值为空，则当前参数不会发送到服务器端,这种指定请求参数的方式和datatables控价类似**/
                params.loginName = $("#q_loginName").val();
                params.phone = $("#q_phone").val();
                params.email = $("#q_email").val();
                params.lockedState = $("#q_lockedState").val();
                params.wxUserOpenId = $("#q_wxUserOpenId").val();
                params.currentPage = params.pageNumber;
                return params;
            }
        });

        /**表单查询**/
        $("#btn_query_sub").click(function () {
            $("#tt_customerList").bootstrapTable('refresh');
        });
    }

    /**
     * 表单异步提交
     */
    function submitForm_sendCouponsManually(){
        var selectedRow= $("#tt_customerList").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            var id = selectedRow[0].customerId;
            $("#customerId").val(id);
            var options = {
                url: $("#form_sendCouponsManually").attr("action"),
                type : "post",
                dataType:"json",
                success : function(data) {
                    if(data.success==true){
                        /** 先重置查询框的值**/
                        resetQueryForm();
                        /** 重新刷新列表数据**/
                        $("#tt").bootstrapTable('refresh');
                        /** 自动关闭弹出模态框窗口**/
                        $(".close").click();
                        showMsg("赠送成功！");
                    }else{
                        showMsg("赠送失败！");
                    }
                }
            };
            $("#form_sendCouponsManually").ajaxSubmit(options);
        } else {
            showMsg("请选择一行记录！");
        }
    }
</script>

<!--手动增送优惠券模态框（Modal） -->
<div class="modal fade" id="sendCouponsManuallyModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:80%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel">手动增送优惠券</h5>
			</div>
			<div class="modal-body">
				<!-- 查询条件 -->
				<div class="panel panel-default">
					<div class="panel-body">
						<form id="qform_sub" class="form-horizontal">
							<div class="form-group" style="margin-top:15px">
								<div class="col-sm-3">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">会员名称</span>
										<input type="text" class="form-control" id="q_loginName" name="loginName" placeholder="请输入会员名称"/>
									</div>
								</div>
								<div class="col-sm-3">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">手机号</span>
										<input type="text" class="form-control" id="q_phone" name="phone" placeholder="请输入手机号"/>
									</div>
								</div>
								<div class="col-sm-3">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">电子邮箱</span>
										<input type="text" class="form-control" id="q_email" name="email" placeholder="请输入电子邮箱"/>
									</div>
								</div>
								<div class="col-sm-2" style="width: 180px;">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">状态</span>
										<select id="q_lockedState" class="form-control">
											<option value="">请选择</option>
											<option value="0">未冻结</option>
											<option value="1">已冻结</option>
										</select>
									</div>
								</div>
								<div class="col-sm-2" style="width: 220px;">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">是否关联微信</span>
										<select id="q_wxUserOpenId" class="form-control">
											<option value="">请选择</option>
											<option value="0">未关联</option>
											<option value="1">已关联</option>
										</select>
									</div>
								</div>
								<div class="col-sm-3">
									<button type="button" id="btn_query_sub" class="btn btn-primary">查询</button>&nbsp;&nbsp;
									<button type="reset" class="btn btn-success" id="resetBtn">重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<form id="form_sendCouponsManually" method="post" action="${basePath}/back/discountCoupon/saveSendDiscountCoupon.sjson">
					<input id="s_discountCouponId" type="hidden" name="discountCouponId" value=""/>
					<input id="customerId" type="hidden" name="customerId" value=""/>
					<table id="tt_customerList"></table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary" onclick="submitForm_sendCouponsManually()">赠送</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
